import { IAction, IState, ITodo, TodoActionType } from '../../../types/todo';

export default function todoReducer(state:IState,action:IAction){
    const {type,payload} = action
    switch (type) {
        case TodoActionType.ADD:
            return {
                ...state,
                todoList:[...state.todoList,payload as ITodo]
            }
        case TodoActionType.COMPLETE:
            return {
                ...state,
                todoList:state.todoList.map((item)=>{
                    return item.id === payload ?
                    {
                        // 修正状态，取反
                        ...item,
                        completed:!item.completed
                    }:{...item}
                })
            }
        case TodoActionType.REMOVE:
            return{
                ...state,
                todoList:state.todoList.filter(item=>payload as number !== item.id)
            }
        default:
            return state;
    }
}